package com.wzx.controller;

import com.wzx.domain.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class Demo02ReturnController {
    //public 返回值类型 方法名(参数类型 参数名){}
    @RequestMapping(path = "demo01.action",method = {RequestMethod.POST,RequestMethod.GET})//回显页面
    public ModelAndView test01(){//
        Person p1 = new Person(1,"jack","1234");
        Person p2 = new Person(2,"rose","1234");
        List<Person> list = new ArrayList<Person>();
        list.add(p1);
        list.add(p2);

        //使用ModelAndView实现请求转发
        ModelAndView mv = new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("demo04_update_persons");//前缀+逻辑视图+后缀
        //物理视图 真正的文件地址 /WEB-INF/jsp/demo04_update_persons.jsp
        //物理视图 = 前缀+逻辑视图+后缀
        return mv;
    }

    @RequestMapping(path="demo02.action",method = {RequestMethod.GET,RequestMethod.POST})
    public void test02(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //请求转发
        req.setAttribute("name","请求转发");
        req.getRequestDispatcher("demo05.jsp").forward(req,resp);
    }

    @RequestMapping(path="demo03.action",method = {RequestMethod.GET,RequestMethod.POST})
    public String test03(Model model){
        //Model 可以设置数据，自动由视图解析器带到页面
        model.addAttribute("data","wHelloWord");
        return "success"; //逻辑视图 文件名

    }

    @RequestMapping(path="demo04.action",method = {RequestMethod.GET,RequestMethod.POST})
    public String test04(Model model){
        //Model 可以设置数据，自动由视图解析器带到页面
        model.addAttribute("data","wHelloWord");
        //return "forward:/WEB-INF/jsp/success.jsp"; //
        //return "redirect:http://www.baidu.com"; //外网
        //return "redirect:demo05.jsp"; //
        return "redirect:demo01.action"; //
    }

    @RequestMapping(path = "demo05.action",method = {RequestMethod.POST,RequestMethod.GET})//回显页面
    public @ResponseBody Object test05(){//

        Person p1 = new Person(1,"jack","1234");
        Person p2 = new Person(2,"rose","1234");
        List<Person> list = new ArrayList<Person>();
        list.add(p1);
        list.add(p2);
        System.out.println("test05 return 之后------------------------");

        return list; //springmvc将 list使用ObjectMapper转成json
    }
    @RequestMapping(path = "demo06.action",method = {RequestMethod.POST,RequestMethod.GET})//回显页面
    public ModelAndView  test06(@RequestBody  Person person){//
        System.out.println("object:"+person);
       return null;
    }

}
